﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RelativeRotater : MonoBehaviour
{
    public float relativeAngleZ
    {
        set
        {
            _relativeAngle.z = value;
            var angle = originAngle.z + _relativeAngle.z;
            if (mirrorOn && mirror != null)
            {
                mirror.localEulerAngles = new Vector3(mirror.localEulerAngles.x,
                    mirror.localEulerAngles.y, -angle);
            }
            transform.localEulerAngles = new Vector3(transform.localEulerAngles.x,
                transform.localEulerAngles.y, angle);
        }

        get { return _relativeAngle.z; }
    }
    public float relativeAngleX
    {
        set
        {
            _relativeAngle.x = value;
            var angle = originAngle.x + _relativeAngle.x;
            Quaternion rot;
            if (mirrorOn && mirror != null)
            {
                mirror.localEulerAngles = originAngle.SetZ(-originAngle.z);//mirrorZ
                rot = Quaternion.AngleAxis(_relativeAngle.x, mirror.right);
                mirror.rotation = rot * mirror.rotation;
            }
            transform.localEulerAngles = originAngle;
            rot = Quaternion.AngleAxis(_relativeAngle.x, transform.right);
            transform.rotation = rot * transform.rotation;
        }

        get { return _relativeAngle.x; }
    }
    public Vector3 originAngle;
    private Vector3 _relativeAngle;
    public Transform mirror;
    public bool mirrorOn = true;
    public void Reset()
    {
        SetOrigin();
    }
    public void SetOrigin()
    {
        originAngle = transform.localEulerAngles;
    }
}
